﻿using System;
using System.Diagnostics.CodeAnalysis;
using System.Windows.Controls;
using Pfz.Factoring;

namespace Pfz.WpfControls.ValueControls
{
	[SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses")]
	[AutoRegisterInControlFactory(typeof(decimal))]
	[AutoRegisterInControlFactory(typeof(decimal?))]
	internal sealed class DecimalControl:
		UserControl,
		IValueControl
	{
		private TextBox _textBox = new TextBox();
		public DecimalControl()
		{
			Content = _textBox;
		}

		public void Clear()
		{
			_textBox.Clear();
		}

		public bool IsReadOnly
		{
			get
			{
				return _textBox.IsReadOnly;
			}
			set
			{
				_textBox.IsReadOnly = value;
			}
		}

		public object Value
		{
			get
			{
				string text = _textBox.Text;
				if (string.IsNullOrWhiteSpace(text))
					return null;

				return Convert.ToDecimal(text);
			}
			set
			{
				if (value == null)
					Clear();
				else
					_textBox.Text = value.ToString();
			}
		}
	}
}
